local skill = fk.CreateSkill{
  name = "tea__feijun",
}
---@type TrigSkelSpec<TrigFunc>
local feijun = {
  anim_type = "drawcard",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skill.name) then
      if player.dead then return end
      return (player:getMark("tea__feijun_record") == 0 and player.phase == Player.Finish)
      or (player:getMark("tea__feijun_record") == player.phase)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local phases = {"准备阶段", "判定阶段", "摸牌阶段", "出牌阶段", "弃牌阶段", "结束阶段"}
    room:notifySkillInvoked(player, self.name, "drawcard")
    player:broadcastSkillInvoke(skill.name, player.phase + 1)
    player:drawCards(3, self.name)
    if not player.dead then
      local cards_num = 0
      local num = #player:getCardIds(Player.Hand) - player.maxHp
      num = math.max(num, 0)
      if num > 0 then
        cards_num = #room:askToDiscard(player, {
          min_num = num,
          max_num = num,
          skill_name = skill.name,
          include_equip = false,
          cancelable = false,
          prompt = "#tea__feijun-ask:::"..tostring(num),
        })
      end
      if cards_num < player.hp and not player.dead then
        room:notifySkillInvoked(player, self.name, "special")
        player:broadcastSkillInvoke(skill.name, table.random({1, 2}))
        local choice = room:askToChoice(player, {
          choices = phases,
          skill_name = skill.name,
          prompt = "#tea__feijun-choose"
        })
        room:setPlayerMark(player, "tea__feijun_record", table.indexOf(phases, choice) + 1)
        room:setPlayerMark(player, "@tea__feijun_shown", choice)
      end
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local feijun_re = {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skill.name)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@tea__feijun_shown", "结束阶段")
  end,
}
skill:addEffect(fk.EventPhaseEnd, feijun)
skill:addEffect(fk.GamePrepared, feijun_re)
return skill